package com.ruoyi.project.order.controller;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.order.commons.OrderConst;
import com.ruoyi.project.order.domain.Order;
import com.ruoyi.project.order.service.IOrderAddService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static com.ruoyi.framework.web.domain.AjaxResult.error;

/**
 * @author jy
 * @version 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderAddController extends BaseController {
    @Autowired
    private IOrderAddService orderAddService;

    /*新增订单页面跳转*/
    @RequiresPermissions("order:add")
    @GetMapping ("/add")
    public String toAdd(){


        return OrderConst.prefix+"/add";
    }
    /*校验项目名称唯一
    *return
    *   0  唯一，可使用
    *   1  不唯一，不能使用
    *
    * */
    @PostMapping("/checkOrderNameUnique")
    @ResponseBody
    public String checkOrderNameUnique(Order order){
        return orderAddService.checkOrderNameUnique(order);
    }

    /**
     * 新增订单
     *
     * @Validated 校验请求参数，配合getset方法上的注解
     * @param order
     * @return
     */
    @RequiresPermissions("order:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Order order){
        if (OrderConst.ORDER_NAME_NOT_UNIQUE.equals(orderAddService.checkOrderNameUnique(order)))
        {
            return error("新增订单'" + order.getOrderName() + "'失败，订单项目名称已存在");
        }
        return toAjax(orderAddService.insertOrder(order));
    }

}
